'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 官阶商店
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var battleConf = require('../../../../config/battleConf');
var genRewardService = require('../../../../services/genRewardService');
var rewardService = require('../../../../services/rewardService');
var roleService = require('../../../../services/roleService');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var property_id = msg.property_id;

    if (!property_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'build', 'battle', 'task', 'achieve', 'daily_task', 'tech', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var battle_model = player_manager.battle;

                var job_type = role_model.getJob();
                var reward_item = battleConf.getPvpShopRewardConf(job_type, property_id);

                if (!reward_item) {
                    return next(null, {
                        code: Code.BATTLE_SHOP_ITEM_NOT_EXIST
                    });
                }

                // 判断官阶是否达到购买条件
                if (reward_item.official_position_id > battle_model.getNoble()) {
                    return next(null, {
                        code: Code.BATTLE_NOBLE_NOT_REACH
                    });
                }

                var reward_price = battleConf.getPvpShopRewardPrice(job_type, property_id);

                // 扣除龙币
                if (reward_price.cost_coin > 0) {
                    if (!roleService.subDragonGold(player_manager, moduleType.MDL_PVP, operationType.OPR_PVP_NOBLE_SHOP_BUY, reward_price.cost_coin)) {
                        return next(null, {
                            code: Code.BATTLE_DRAGON_COIN_NOT_ENOUGH
                        });
                    }
                }

                // 扣除钻石
                if (reward_price.cost_point > 0) {
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_PVP, operationType.OPR_PVP_NOBLE_SHOP_BUY, reward_price.cost_point)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }
                }

                // 添加奖励
                var reward = genRewardService.generateReward(reward_item);
                rewardService.addRewardToRole(player_manager, reward, moduleType.MDL_PVP, operationType.OPR_PVP_NOBLE_SHOP_BUY);

                // 增加副本统计
                taskService.addUpgradeConsume(player_manager);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
